package com.stedu.mall.goodsservice.controller;

import com.stedu.mall.common.bean.Category;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.MallException;
import com.stedu.mall.common.service.CategoryService;
import com.stedu.mall.common.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //添加
    @PostMapping
    public RespBean insert(@RequestBody Category category) throws MallException {
        categoryService.insert(category);

        return RespBean.ok("添加成功");
    }
    //根据id删除
    @DeleteMapping(("/{id}"))
    public RespBean delete(@PathVariable("id") Integer id) throws MallException {
        categoryService.delete(id);

        return RespBean.ok("删除成功");
    }
    //根据id修改
    @PutMapping
    public RespBean update(@RequestBody Category category) throws MallException {
        categoryService.update(category);

        return RespBean.ok("修改成功");
    }
    //条件分页搜索
    @GetMapping("/search")
    public RespBean selectByPage (Category category, Integer pageNum, Integer pageSize){

        Object data = categoryService.selectByCondition(category, pageNum, pageSize);

        return RespBean.ok("",data);
    }
    //根据id查询
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id){
        return RespBean.ok("",categoryService.selectById(id));
    }
    //获取所有的父分类
    @GetMapping("/allparent")
    public RespBean selectAllParent(){
        Category category = new Category();
        category.setParentId(0);
        Object data = categoryService.selectByCondition(category, null, null);

        return RespBean.ok("",data);
    }

}
